/**
 * This file is part of Tamariba. Copyright (C) 2011 Michael Vogt <michael@tamariba.eu>, Patrick Hempel
 * <patrick@tamariba.eu>
 * 
 * Tamariba is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
 * 
 * Tamariba is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with Tamariba. If not, see
 * <http://www.gnu.org/licenses/>.
 */
package eu.tamariba.html.user.client.ioc;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.event.shared.SimpleEventBus;
import com.google.gwt.inject.client.AbstractGinModule;
import com.google.gwt.place.shared.PlaceController;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;

import eu.tamariba.html.client.scaffold.gae.GaeAuthRequestTransport;
import eu.tamariba.html.client.scaffold.request.EventSourceRequestTransport;
import eu.tamariba.html.user.client.request.UserRequestFactory;

public class UserModule extends AbstractGinModule {
    @Override
    protected void configure() {
        bind(EventBus.class).to(SimpleEventBus.class).in(Singleton.class);
        bind(UserRequestFactory.class).toProvider(RequestFactoryProvider.class).in(Singleton.class);
        bind(PlaceController.class).toProvider(PlaceControllerProvider.class).in(Singleton.class);
    }

    static class PlaceControllerProvider implements Provider<PlaceController> {

        private final PlaceController placeController;

        @Inject
        public PlaceControllerProvider(EventBus eventBus) {
            this.placeController = new PlaceController(eventBus);
        }

        public PlaceController get() {
            return placeController;
        }
    }

    static class RequestFactoryProvider implements Provider<UserRequestFactory> {

        private final UserRequestFactory requestFactory;

        @Inject
        public RequestFactoryProvider(EventBus eventBus) {
            requestFactory = GWT.create(UserRequestFactory.class);
            requestFactory.initialize(eventBus, new EventSourceRequestTransport( eventBus, new GaeAuthRequestTransport(eventBus)));
        }

        public UserRequestFactory get() {
            return requestFactory;
        }
    }
}